﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Capa_Negocios;

namespace Calendarizacion_URL
{
    public partial class FormLaboratorio : Form
    {
        Form padre;
        int fila;
        public FormLaboratorio(Form padre)
        {
            InitializeComponent();
            this.padre = padre;
            fila = 0;
            Listar();
        }

        private void Listar()
        {
            this.Size = new Size(1027, 647);
            dataGridView1.DataSource = Laboratorio.listarLabs();
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[1].HeaderText = "Nombre";
            dataGridView1.Columns[2].HeaderText = "Capacidad";
            dataGridView1.Columns[3].HeaderText = "Encargado(a)";
            dataGridView1.Columns[4].Visible = false;
            dataGridView1.Columns[5].HeaderText = "Edificio";
            groupBox2.Visible = true;
            groupBox1.Hide();
        }

        private void editarLaboratorioToolStripMenuItem_Click(object sender, EventArgs e)
        {

            groupBox1.Visible = true;
            groupBox2.Visible = false;
        }

        private void desactivarLaboratorioToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            groupBox2.Visible = true;
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            padre.Enabled = true;
            this.Dispose();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            //Búsqueda por nombre de laboratorio
            if (radioButton2.Checked)
                dataGridView1.DataSource = Laboratorio.filtroNombreLab(textBox4.Text);
            //Búsqueda por edificio
            else
                dataGridView1.DataSource = Laboratorio.filtroEdificio(textBox4.Text);
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[4].Visible = true;
            dataGridView1.Columns[1].HeaderText = "Nombre";
            dataGridView1.Columns[2].HeaderText = "Capacidad";
            dataGridView1.Columns[3].HeaderText = "Encargado(a)";
            dataGridView1.Columns[4].HeaderText = "Edificio";
            dataGridView1.Columns[5].Visible = false;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.Size = new Size(435, 647);
            fila = e.RowIndex;
            editar();
        }

        private void editar()
        {
            groupBox2.Hide();
            this.Size = new Size(438, 661);
            groupBox1.Visible = true;
            groupBox1.Text = "Editar usuario";
            textBox1.Text = dataGridView1[1, fila].Value.ToString();
            textBox2.Text = dataGridView1[3, fila].Value.ToString();
            textBox3.Text = dataGridView1[2, fila].Value.ToString();
            comboBox2.Text = dataGridView1[5, fila].Value.ToString();
            richTextBox1.Text = dataGridView1[4, fila].Value.ToString();
            //Edificios...
            listarEdificios();
        }

        private void listarEdificios()
        {
            comboBox2.Items.Clear();
            List<object> edificios = Edificio.nombreEdificios();
            string[] temp = new string[2];
            for (int i = 0; i < edificios.Count; i++)
            {
                temp = edificios.ElementAt(i).ToString().Split(',');
                comboBox2.Items.Add(temp[1]);
            }
        }

        private void FormLaboratorio_FormClosing(object sender, FormClosingEventArgs e)
        {
            padre.Enabled = true;
            this.Dispose();
        }

        private void menúPrincipalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            padre.Enabled = true;
            this.Dispose();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            Listar();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            comboBox2.Text = "";
            richTextBox1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /****Nuevo laboratorio*****/
            if (!groupBox1.Text.Equals("Editar usuario"))
                Laboratorio.insertar(comboBox2.Text, textBox1.Text, int.Parse(textBox3.Text),
                                    textBox2.Text, richTextBox1.Text);                
            /****Modificación de laboratorio*****/
            else
                Laboratorio.editar(int.Parse(dataGridView1[0, fila].Value.ToString()), comboBox2.Text, textBox1.Text,
                                    int.Parse(textBox3.Text), textBox2.Text, richTextBox1.Text, true);         
        }

        private void listarToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            groupBox1.Text = "Ingresar nuevo";
            groupBox1.Visible = true;
            groupBox2.Hide();
            this.Size = new Size(435, 647);
            listarEdificios();
        }

        private void nuevoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Listar();
        }
    }
}
